package com.okyai.cms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.cms.domain.CmsHistoryLog;
import com.okyai.cms.service.ICmsHistoryLogService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.core.page.TableDataInfo;

/**
 * 文章浏览记录Controller
 * 
 * @author okyai
 * @date 2020-12-03
 */
@Controller
@RequestMapping("/cms/log")
public class CmsHistoryLogController extends BaseController
{
    private String prefix = "cms/log";

    @Autowired
    private ICmsHistoryLogService cmsHistoryLogService;

    @RequiresPermissions("cms:log:view")
    @GetMapping()
    public String log()
    {
        return prefix + "/log";
    }

    /**
     * 查询文章浏览记录列表
     */
    @RequiresPermissions("cms:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsHistoryLog cmsHistoryLog)
    {
        startPage();
        List<CmsHistoryLog> list = cmsHistoryLogService.selectCmsHistoryLogList(cmsHistoryLog);
        return getDataTable(list);
    }

    /**
     * 导出文章浏览记录列表
     */
    @RequiresPermissions("cms:log:export")
    @Log(title = "文章浏览记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsHistoryLog cmsHistoryLog)
    {
        List<CmsHistoryLog> list = cmsHistoryLogService.selectCmsHistoryLogList(cmsHistoryLog);
        ExcelUtil<CmsHistoryLog> util = new ExcelUtil<CmsHistoryLog>(CmsHistoryLog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 新增文章浏览记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文章浏览记录
     */
    @RequiresPermissions("cms:log:add")
    @Log(title = "文章浏览记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsHistoryLog cmsHistoryLog)
    {
        return toAjax(cmsHistoryLogService.insertCmsHistoryLog(cmsHistoryLog));
    }

    /**
     * 修改文章浏览记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsHistoryLog cmsHistoryLog = cmsHistoryLogService.selectCmsHistoryLogById(id);
        mmap.put("cmsHistoryLog", cmsHistoryLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章浏览记录
     */
    @RequiresPermissions("cms:log:edit")
    @Log(title = "文章浏览记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsHistoryLog cmsHistoryLog)
    {
        return toAjax(cmsHistoryLogService.updateCmsHistoryLog(cmsHistoryLog));
    }

    /**
     * 删除文章浏览记录
     */
    @RequiresPermissions("cms:log:remove")
    @Log(title = "文章浏览记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsHistoryLogService.deleteCmsHistoryLogByIds(ids));
    }
}
